package com.joker.demo.controller;


import com.joker.demo.domain.LawPrompt;
import com.joker.demo.service.PromptService;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.input.Prompt;
import dev.langchain4j.model.input.PromptTemplate;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author : feixiang.li
 * @since : 2025-09-25 18:00
 */
@RestController
@RequestMapping("/law")
public class LawController {

    @Autowired
    private PromptService promptService;


    @RequestMapping("/ask")
    public String ask(@RequestParam(value = "question", defaultValue = "你是谁") String question) {
        return promptService.doc(question, 500, "markdown");
    }


    @RequestMapping("/chat")
    public String chat(@RequestParam(value = "question", defaultValue = "你是谁") String question) {
        LawPrompt lawPrompt = new LawPrompt();
        lawPrompt.setPrompt(question);
        lawPrompt.setLength(500);
        lawPrompt.setFormat("markdown");
        return promptService.chat(lawPrompt);
    }


    @Resource
    private ChatModel chatModel;
    
    @RequestMapping("/chat3")
    public String chat3() {
        PromptTemplate promptTemplate = PromptTemplate.from("请回一下法律问题，{{question}}，限制在多少字符以内{{length}}");

        Prompt prompt = promptTemplate.apply(Map.of("question", "你是谁", "length", 500));
        UserMessage userMessage = prompt.toUserMessage();
        ChatResponse chatResponse = chatModel.chat(userMessage);
        System.out.println(chatResponse.aiMessage().text());
        return chatResponse.aiMessage().text();
    }
}
